library("phyloseq")

library("tidyverse")

library("RColorBrewer")

library("dplyr")

library("MicrobiotaProcess")


setwd("E:/projects/R_draw_sc/test/lefse/phyloseq")
otu <- "otu_table.qza"
rep <- "rep-seqs.qza"
tree <- "rooted-tree.qza"
tax <- "taxonomy.qza"
sample <- "group.txt"
ps_dada2 <- import_qiime2(otuqza = otu, taxaqza = tax, refseqqza = rep,
                          mapfilename = sample, treeqza = tree)

set.seed(1024)
dd <- phyloseq::rarefy_even_depth(ps_dada2)
table(sample_data(dd)$group)

diffres <- diff_analysis(obj = ps_dada2, classgroup = "group")

diffcladeplot <- ggdiffclade(obj = diffres,
                             alpha = 0.3, size = 0.2,
                             skpointsize = 0.6,
                             taxlevel = 3,
                             settheme = FALSE,
                             setColors = FALSE) +
  guides(color = guide_legend(keywidth = 0.1,
                              keyheight = 0.6,
                              order = 3,
                              ncol = 1)) +
  theme(panel.background = element_rect(fill = NA),
        legend.position = "right",
        plot.margin = margin(0, 0, 0, 0),
        legend.spacing.y = unit(0.02, "cm"),
        legend.title = element_text(size = 7),
        legend.text = element_text(size = 6),
        legend.box.spacing = unit(0.02, "cm"))
diffcladeplot
#ggsave("C:/Users/Administrator/Desktop/kk/zz.pdf",diffcladeplot)


#set.seed(1024)
#p_rare <- ggrarecurve(obj=ps_dada2,
#                      indexNames=c("Observe","Chao1","ACE"),
#                      chunks=300) +
#  theme(legend.spacing.y=unit(0.02,"cm"),
#        legend.text=element_text(size=6))
#p_rare

#otubar <- ggbartax(obj=ps_dada2) +
#  xlab(NULL) +
#  ylab("relative abundance (%)")
#otubar

#phytax <- get_taxadf(obj=ps_dada2, taxlevel=2)
#phybar <- ggbartax(obj=phytax) +
#  xlab(NULL) + ylab("relative abundance (%)")
#phybar

#alphaobj <- get_alphaindex(ps_dada2)
#head(as.data.frame(alphaobj))
#
#p_alpha <- ggbox(alphaobj, geom="violin", factorNames="group") +
#  theme(strip.background = element_rect(colour=NA, fill="grey"))
#p_alpha

#phybar2 <- ggbartax(obj=phytax, facetNames="group", count=TRUE) + xlab(NULL) + ylab("abundance")
#phybar2


#pcares <- get_pca(obj=ps_dada2, method="hellinger")
## Visulizing the result
#pcaplot <- ggordpoint(obj=pcares, biplot=TRUE, speciesannot=TRUE,
#                      factorNames=c("sample"), ellipse=TRUE)
#pcaplot


#pcoares <- get_pcoa(obj=ps_dada2, distmethod="euclidean", method="hellinger")
## Visualizing the result
#pcoaplot <- ggordpoint(obj=pcoares, biplot=TRUE, speciesannot=TRUE,
#                       factorNames=c("group"), ellipse=TRUE)
#pcoaplot

#hcsample <- get_clust(obj=ps_dada2, distmethod="euclidean",
#                      method="hellinger", hclustmethod="average")
## rectangular layout
#clustplot1 <- ggclust(obj=hcsample,
#                      layout = "rectangular",
#                      pointsize=1,
#                      fontsize=0,
#                      factorNames=c("group"))
#  #theme_tree2(legend.position="right",
#  #            plot.title = element_text(face="bold", lineheight=25,hjust=0.5))
#clustplot1

#kostic2012crc <- phyloseq::rarefy_even_depth(ps_dada2,rngseed=1024)
#table(sample_data(kostic2012crc)$group)
#
#set.seed(1024)
#diffres <- diff_analysis(obj=kostic2012crc, classgroup="group",
#                         mlfun="lda",
#                         filtermod="fdr",
#                         firstcomfun = "kruskal.test",
#                         firstalpha=0.05,
#                         strictmod=TRUE,
#                         secondcomfun = "wilcox.test",
#                         subclmin=3,
#                         subclwilc=TRUE,
#                         secondalpha=0.01,
#                         lda=3)
#diffres

#diffcladeplot <- ggdiffclade(obj=diffres,
#                             settheme=FALSE,
#                             setColors=FALSE)
#  guides(color = guide_legend(keywidth = 0.1,
#                              keyheight = 0.6,
#                              order = 3,
#                              ncol=1)) +
#  theme(panel.background=element_rect(fill=NA),
#        legend.position="right",
#        plot.margin=margin(0,0,0,0),
#        legend.spacing.y = unit(0.02, "cm"),
#        legend.title=element_text(size=7),
#        legend.text=element_text(size=6),
#        legend.box.spacing=unit(0.02,"cm"))

